﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveCharts;
using LiveCharts.Wpf;
using MyFirstWPF.View;
using MyFirstWPF.ViewModel;

namespace MyFirstWPF.Common
{
    public class Page3Model
    {
        public static string dbPath = "../../studyPlan.db";
        SeriesCollection lineSeriesCollection1 = new SeriesCollection();
        List<string> _lineXLabels1 = new List<string>();
        SeriesCollection lineSeriesCollection2 = new SeriesCollection();
        List<string> _lineXLabels2 = new List<string>();
        SeriesCollection lineSeriesCollection3 = new SeriesCollection();
        List<string> _lineXLabels3 = new List<string>();

        SeriesCollection pieSeriesCollection = new SeriesCollection();

        public Page3Model()
        {
            GetLineSeries1Data();
            GetLineSeries2Data();
            GetLineSeries3Data();
            GetPieSeriesData();
        }
        #region 属性
        /// <summary>
        /// 折线图集合
        /// </summary>
        public SeriesCollection LineSeriesCollection1
        {
            get
            {
                return lineSeriesCollection1;
            }

            set
            {
                lineSeriesCollection1 = value;
            }
        }
        public SeriesCollection LineSeriesCollection2
        {
            get
            {
                return lineSeriesCollection2;
            }

            set
            {
                lineSeriesCollection2 = value;
            }
        }


        /// <summary>
        /// 柱状图集合
        /// </summary>
        //public SeriesCollection ColunmSeriesCollection
        //{
        //    get
        //    {
        //        return colunmSeriesCollection;
        //    }

        //    set
        //    {
        //        colunmSeriesCollection = value;
        //    }
        //}

        /// <summary>
        /// 饼图图集合
        /// </summary>
        public SeriesCollection PieSeriesCollection
        {
            get
            {
                return pieSeriesCollection;
            }

            set
            {
                pieSeriesCollection = value;
            }
        }

        /// <summary>
        /// 折线图X坐标
        /// </summary>
        public List<string> LineXLabels1
        {
            get
            {
                return _lineXLabels1;
            }

            set
            {
                _lineXLabels1 = value;
            }
        }
        public List<string> LineXLabels2
        {
            get
            {
                return _lineXLabels2;
            }

            set
            {
                _lineXLabels2 = value;
            }
        }



        /// <summary>
        /// 柱状图X坐标
        /// </summary>
        //public List<string> ColumnXLabels
        //{
        //    get
        //    {
        //        return _columnXLabels;
        //    }

        //    set
        //    {
        //        _columnXLabels = value;
        //    }
        //}
        #endregion


        #region 方法
        ///<summary>
        ///折线图1的数据
        ///</summary>
        private void GetLineSeries1Data()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                string title = "预计专注时间曲线";
                List<string> _dates = new List<string>();
                _dates = GetCurrentWeekDates();
                // 查询
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //数据取出
                List<double> values = new List<double>();
                for (int j = 0; j < 7; j++)
                {
                    double value = 0;
                    double _totalHours = 0;
                    for (int i = 0; i < taskList.Count; i++)
                    {
                        TimeSpan timeSpan = DateTime.Parse(taskList[i].EndTime) - DateTime.Parse(taskList[i].StartTime);
                        TimeSpan timeSpan1 = DateTime.Parse(taskList[i].EndTime).TimeOfDay - DateTime.Parse(taskList[i].StartTime).TimeOfDay;
                        int min = DateTime.Parse(taskList[i].StartTime).Date.CompareTo(DateTime.Parse(_dates[j]).Date);
                        int max = DateTime.Parse(taskList[i].EndTime).Date.CompareTo(DateTime.Parse(_dates[j]).Date);
                        if (DateTime.Parse(_dates[j]).Date == DateTime.Parse(taskList[i].EndTime).Date
                            && timeSpan.TotalHours<=24)
                        {
                            _totalHours += timeSpan.TotalHours;
                        }
                        else if (timeSpan.TotalHours > 24&&min == -1 && (max == 1 || max == 0))
                        {
                            _totalHours += timeSpan1.TotalHours;
                        }
                    }
                    value = double.Parse(_totalHours.ToString("0.00"));
                    values.Insert(j, value);
                }
                //布置数据、包含一周的日期数组
                LineSeries lineseries = new LineSeries();
                lineseries.DataLabels = true;
                lineseries.Title = title;
                lineseries.Values = new ChartValues<double>(values);
                LineSeriesCollection1.Add(lineseries);
                 
                for (int i = 0; i < 7; i++)
                {
                    LineXLabels1.Add(_dates[i]);
                }
            }
        }
        private void GetLineSeries2Data()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                string title = "实际专注时间曲线";
                List<string> _dates = new List<string>();
                _dates = GetCurrentWeekDates();
                // 查询
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //数据取出
                List<double> values = new List<double>();
                for (int j = 0; j < 7; j++)
                {
                    double value = 0;
                    double _totalHours = 0;
                    for (int i = 0; i < taskList.Count; i++)
                    {
                        TimeSpan timeSpan = DateTime.Parse(taskList[i].EndTime) - DateTime.Parse(taskList[i].StartTime);
                        TimeSpan timeSpan1 = DateTime.Parse(taskList[i].EndTime).TimeOfDay - DateTime.Parse(taskList[i].StartTime).TimeOfDay;
                        int min = DateTime.Parse(taskList[i].StartTime).Date.CompareTo(DateTime.Parse(_dates[j]).Date);
                        int max = DateTime.Parse(taskList[i].EndTime).Date.CompareTo(DateTime.Parse(_dates[j]).Date);
                        if(taskList[i].isFinished == 1)
                        {
                            if (DateTime.Parse(_dates[j]).Date == DateTime.Parse(taskList[i].EndTime).Date
                            && timeSpan.TotalHours <= 24)
                            {
                                _totalHours += timeSpan.TotalHours;
                            }
                            else if (timeSpan.TotalHours > 24 && min == -1 && (max == 1 || max == 0))
                            {
                                _totalHours += timeSpan1.TotalHours;
                            }
                        }
                    }
                    value = double.Parse(_totalHours.ToString("0.00"));
                    values.Insert(j, value);
                }
                //布置数据、包含一周的日期数组
                LineSeries lineseries = new LineSeries();
                lineseries.DataLabels = true;
                lineseries.Title = title;
                lineseries.Values = new ChartValues<double>(values);
                LineSeriesCollection1.Add(lineseries);

                for (int i = 0; i < 7; i++)
                {
                    LineXLabels1.Add(_dates[i]);
                }
            }
        }
        void GetLineSeries3Data()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                string title = "任务完成度/%";
                List<string> _dates = new List<string>();
                _dates = GetCurrentWeekDates();
                //查询
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //数据取出
                List<double> values = new List<double>();
                for (int j = 0; j < 7; j++)
                {
                    double value = 0;
                    double _extent = 0;
                    double _lastExtent = 0;
                    double total = 0;
                    for (int i = 0; i < taskList.Count; i++)
                    {
                        if (DateTime.Parse(_dates[j]).Date == DateTime.Parse(taskList[i].StartTime).Date) total++;
                        if (DateTime.Parse(_dates[j]).Date == DateTime.Parse(taskList[i].StartTime).Date
                            && taskList[i].isFinished == 1) value++;
                    }
                    if(total!=0)_extent = value / total;
                    _lastExtent = double.Parse(_extent.ToString("0.0000"))* 100;
                    values.Insert(j, _lastExtent);
                }
                //布置数据、包含一周的日期数组
                LineSeries lineseries = new LineSeries();
                lineseries.DataLabels = true;
                lineseries.Title = title;
                lineseries.Values = new ChartValues<double>(values);
                LineSeriesCollection2.Add(lineseries);

                for (int i = 0; i < 7; i++)
                {
                    LineXLabels2.Add(_dates[i]);
                }
            }
                
        }
        void GetPieSeriesData()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                List<string> titles = new List<string> { "已完成", "未完成" };
                ChartValues<double> chartvalue = new ChartValues<double>();
                //查询
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //数据取出
                List<double> pieValues = new List<double>();
                double _checkedTasks = 0;
                double _extent = 0;
                double _lastExtent = 0;
                for (int i = 0; i < taskList.Count; i++)
                {
                    if (taskList[i].isFinished != 0) _checkedTasks++;
                }
                if (taskList.Count != 0) _extent = _checkedTasks / double.Parse(taskList.Count.ToString());
                _lastExtent = double.Parse(_extent.ToString("0.00"));
                pieValues.Add(_lastExtent * 100);
                pieValues.Add(100 - _lastExtent * 100);
                //布置数据
                for (int i = 0; i < titles.Count; i++)
                {
                    chartvalue = new ChartValues<double>();
                    chartvalue.Add(pieValues[i]);
                    PieSeries series = new PieSeries();
                    series.DataLabels = true;
                    series.Title = titles[i];
                    series.Values = chartvalue;
                    PieSeriesCollection.Add(series);
                }
            }   
        }
       
        List<string> GetCurrentWeekDates()
        {
            List<string> dates = new List<string>();
            DateTime dt = DateTime.Now;
            int year = dt.Year;
            int month = dt.Month;
            //int days = DateTime.DaysInMonth(year, mouth);
            //从现在向前追溯7天      
            DateTime dt_First = dt.AddDays(-7);
            dates.Add(String.Format("{0:d}", dt_First.Date));

            for (int i = 1; i < 7; i++)
            {
                DateTime temp = dt_First.AddDays(i);
                dates.Add(String.Format("{0:d}", temp.Date));
            }
            return dates;
        }
        #endregion
    }
}

